<?php

/**
 * This is the model class for table "{{menu_item}}".
 *
 * The followings are the available columns in table '{{menu_item}}':
 * @property integer $item_id
 * @property integer $parent_id
 * @property integer $menu_id
 * @property string $label
 * @property string $url
 * @property string $description
 * @property string $date_added
 * @property string $last_updated
 * @property integer $sort_order
 * @property string $status
 *
 * The followings are the available model relations:
 * @property Menu $menu
 * @property MenuItem $parent
 * @property MenuItem[] $menuItems
 */
class MenuItem extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return MenuItem the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{menu_item}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('menu_id, label, url, description, date_added, last_updated, sort_order, status', 'required'),
			array('parent_id, menu_id, sort_order', 'numerical', 'integerOnly'=>true),
			array('label', 'length', 'max'=>255),
			array('status', 'length', 'max'=>8),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('item_id, parent_id, menu_id, label, url, description, date_added, last_updated, sort_order, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'menu' => array(self::BELONGS_TO, 'Menu', 'menu_id'),
			'parent' => array(self::BELONGS_TO, 'MenuItem', 'parent_id'),
			'menuItems' => array(self::HAS_MANY, 'MenuItem', 'parent_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'item_id' => 'Item',
			'parent_id' => 'Parent',
			'menu_id' => 'Menu',
			'label' => 'Label',
			'url' => 'Url',
			'description' => 'Description',
			'date_added' => 'Date Added',
			'last_updated' => 'Last Updated',
			'sort_order' => 'Sort Order',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('item_id',$this->item_id);
		$criteria->compare('parent_id',$this->parent_id);
		$criteria->compare('menu_id',$this->menu_id);
		$criteria->compare('label',$this->label,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('date_added',$this->date_added,true);
		$criteria->compare('last_updated',$this->last_updated,true);
		$criteria->compare('sort_order',$this->sort_order);
		$criteria->compare('status',$this->status,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function getItems($menu_id, $parent_id=null){
        $results = Yii::app()->getDb()->createCommand();
        $results->select('item_id, label, url')->from('{{menu_item}}');

        if($parent_id==null)
            $results->where('menu_id=:mid AND parent_id IS NULL', array(':mid'=>(int)$menu_id));
        else
            $results->where('menu_id=:mid AND parent_id=:pid', array(':mid'=>(int)$menu_id, ':pid'=>$parent_id));

        $results->order('sort_order ASC, label ASC');
        $results = $results->queryAll();

        $items = array();

        if(empty($results))
            return $items;

        foreach($results AS $result){
            $childItems = $this->getItems($menu_id, $result['item_id']);
            $items[] = array(
                'label' => $result['label'],
                'url'   => $result['url'],
                'itemOptions'=>array('class'=>'listItem'),
                'linkOptions'=>array('class'=>'listItemLink', 'title'=>$result['label']),
                'submenuOptions'=>array(),
                'items'=>$childItems,
            );

            return $items;
        }
    }
}